package mail.verify.slider;

import mail.entity.ProxyEntity;
import mail.util.UnZipper;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ProxySelector;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
import java.util.Objects;

import static com.han.http.util.Print.printlnInfo;

/**
 * @author: Hanjiafeng
 * @date: Created in 16:15 2019/2/26
 * @desc: 第三方验证机制
 */
public class ThirdPart {

    private HttpClient client;

    private HttpRequest.Builder builder;

    private ThirdPart(ProxyEntity proxyEntity) {
        HttpClient.Builder clientBuilder = HttpClient.newBuilder();
        if (!Objects.isNull(proxyEntity)) {
            //设置代理
            clientBuilder.proxy(ProxySelector.of(new InetSocketAddress(proxyEntity.getIp(), proxyEntity.getPort())));
        }
        client = clientBuilder
                .build();
        builder = HttpRequest.newBuilder()
                .timeout(Duration.ofMinutes(1));
    }

    public static String qqMailSlide(String uid, String capCd, ProxyEntity proxyEntity) throws IOException, InterruptedException {

        ThirdPart thirdPart = new ThirdPart(proxyEntity);
        String verifyCode;
        //获取服务器地址
        String addr = thirdPart.getAddress();
        //请求数据
        verifyCode = thirdPart.getVerifyCode(addr, uid, capCd);
        printlnInfo(verifyCode);

        return verifyCode;
    }

    public static void main(String... args) throws Exception {

        ThirdPart thirdPart = new ThirdPart(new ProxyEntity("", "1.195.163.137", 16666, "", ""));
        String url = thirdPart.getAddress();
        printlnInfo(url);
    }

    /**
     * 获取第三方地址
     *
     * @return 地址
     * @throws IOException          io
     * @throws InterruptedException in
     */
    private String getAddress() throws IOException, InterruptedException {
        //获取服务器地址
        HttpRequest request = builder
                .copy()
                .uri(URI.create("http://api.jjsou.cn/getreqsvr"))
                .build();
        HttpResponse<byte[]> response = client.send(request, HttpResponse.BodyHandlers.ofByteArray());
        String addr = new String(UnZipper.unGzipByBytes(response.body()));
        //如果没有返回ip:port,返回null
        if (!addr.matches("[\\d]{1,3}.[\\d]{1,3}.[\\d]{1,3}.[\\d]{1,3}:[\\d]{1,5}")) {
            throw new IOException("没有获取到地址:" + addr);
        }

        return addr;
    }

    /**
     * 发送uid和capCd获取验证码
     *
     * @param addr  地址
     * @param uid   uid
     * @param capCd capCd
     * @return 验证码
     * @throws IOException          io
     * @throws InterruptedException in
     */
    private String getVerifyCode(String addr, String uid, String capCd) throws IOException, InterruptedException {

        String uri = "http://" + addr + "/reqcode";
        HttpRequest request = builder
                .copy()
                .setHeader("Content-Type", "application/x-www-form-urlencoded")
                .uri(URI.create(uri))
                .method("POST", HttpRequest.BodyPublishers.ofString("acc=hulushuju&pwd=hulushuju&mode=1&codedata={\"aid\":\"522005705\",\"cap_cd\":\"" + capCd + "\",\"uid\":\"" + uid + "\",\"referer\":\"https://mail.qq.com/\"}"))
                .build();
        HttpResponse<byte[]> response = client.send(request, HttpResponse.BodyHandlers.ofByteArray());
        String result = new String(UnZipper.unGzipByBytes(response.body()));
        if (!result.contains("randstr")) {
            throw new IOException("没有获取到验证码:" + result);
        }
        return result;
    }
}
